{
 "metadata": {
  "name": "fidelity"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from sympy import *\n",
      "from sympy.physics.quantum import *\n",
      "from sympy.physics.quantum.density import *\n",
      "from sympy.physics.quantum.spin import (\n",
      "    Jx, Jy, Jz, Jplus, Jminus, J2,\n",
      "    JxBra, JyBra, JzBra,\n",
      "    JxKet, JyKet, JzKet,\n",
      ")\n",
      "from IPython.core.display import display_pretty\n",
      "from sympy.physics.quantum.operator import *\n",
      "\n",
      "%load_ext sympyprinting"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "##Fidelity using some kets"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "up = JzKet(S(1)/2,S(1)/2)\n",
      "down = JzKet(S(1)/2,-S(1)/2)\n",
      "amp = 1/sqrt(2)\n",
      "updown = (amp * up ) + (amp * down)\n",
      "\n",
      "# represent turns Kets into matrices\n",
      "up_dm = represent(up * Dagger(up))\n",
      "down_dm = represent(down * Dagger(down)) \n",
      "updown_dm = represent(updown * Dagger(updown))\n",
      "updown2 = (sqrt(3)/2 )* up + (1/2)*down\n",
      "\n",
      "\n",
      "display_pretty(fidelity(up_dm, up_dm))\n",
      "display_pretty(fidelity(up_dm, down_dm)) #orthogonal states\n",
      "display_pretty(fidelity(up_dm, updown_dm).evalf())\n",
      "\n",
      "\n",
      "# alternatively, puts Kets into Density object and compute fidelity\n",
      "d1 = Density( [updown, 0.25], [updown2, 0.75])\n",
      "d2 = Density( [updown, 0.75], [updown2, 0.25])\n",
      "display_pretty(fidelity(d1, d2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "display_data",
       "text": [
        "1"
       ]
      },
      {
       "output_type": "display_data",
       "text": [
        "0"
       ]
      },
      {
       "output_type": "display_data",
       "text": [
        "0.707106781186548"
       ]
      },
      {
       "output_type": "display_data",
       "text": [
        "0.817293551913876"
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## Fidelity on states as Qubits"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "\n",
      "from sympy.physics.quantum.qubit import Qubit\n",
      "state1 = Qubit('0')\n",
      "state2 = Qubit('1')\n",
      "state3 = (1/sqrt(2))*state1 + (1/sqrt(2))*state2\n",
      "state4 = (sqrt(S(2)/3))*state1 + (1/sqrt(3))*state2\n",
      "\n",
      "state1_dm = Density([state1, 1])\n",
      "state2_dm = Density([state2, 1])\n",
      "state3_dm = Density([state3, 1])\n",
      "\n",
      "# mixed qubit states in density\n",
      "d1 = Density([state3, 0.70], [state4, 0.30])\n",
      "d2 = Density([state3, 0.20], [state4, 0.80])\n",
      "\n",
      "\n",
      "display_pretty(fidelity(d1, d2))\n",
      "\n"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "display_data",
       "text": [
        "0.996370452558227"
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}